<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <canvas id="canvas" width="500" height="500"></canvas>

    <script>
        function draw() {
            //获取元素对象
            var canvas = document.getElementById('canvas');
            if (canvas.getContext) {
                //获取画笔对象
                var ctx = canvas.getContext('2d');
                //填充矩形
                ctx.fillRect(25, 25, 100, 100);
                ctx.clearRect(45, 45, 60, 60);
                ctx.strokeRect(50, 50, 50, 50);
            }
        }
        draw();
    </script>
</body>

</html>